<?php
//include_once("model/itemConfig.php");
//var_dump($organization->getAuthorized());
		    global $itemType;
			global $itemTopSize;
			global $itemSize;
			global $itemColor;
			global $itemMaterial;
			global $itemSeason;
			global $itemYear;
			global $itemType;
			global $itemDesigner;
			global $organizationType;
?>
<script>
var userid="<?php echo $currentUser->id;?>";
var orgid="<?php echo $organization->id; ?>";
var myRequest=new stackRequest("organization_distribute",userid,orgid,new Array());

function addPlan()
{
	//$('input:radio[name=inv_id]:checked').parent().next.css("border","3px solid red");
	
	//console.log($('input:radio[name=inv_id]:checked').parent().next().next().text());
	var scan_code=$('input:radio[name=inv_id]:checked').parent().next().next().text();
	var inv_id=$('input:radio[name=inv_id]:checked').val();
	//console.log(scan_code);
	var toorgid=$('#target').val();
	var toorgname=$('#target option:selected').html();
	
	console.log("target id is"+toorgid);
	console.log("target id name"+toorgname);
	if(scan_code==""){
		var num=null;
	}else{
		var num=prompt("请输入计划要添加的产品  "+scan_code+" 的数量","0");
	}
	
	if(num!=null){
			if(num!=0&&$.isNumeric(num) ){
				
				//
				var siblings=$('input:radio[name=inv_id]:checked').parent().siblings();
				var max=-1;
				
				
				var para="";
				siblings.each(function(index){
					console.log($(this).text());
					if(max==-1){
							max=$(this).text();
					
					}else{
					para+="'"+$(this).text()+"',";
					}
					//console.log($(this).text());
				});
				if(parseInt(num)>parseInt(max)){
					alert("您要分配的货物"+scan_code+"的数量大于现有货存数量\n请重新选择和输入"+num+" > "+max);
					return;
				}
				
				para="myRequest.add(new iventory("+para+userid+","+toorgid+","+num+",0,"+inv_id+"));"
				console.log(para);
				eval(para);
				//alert($('.change').text());
					$('.change').html(toorgname);
					$('.change').attr('class',' ');
				//console.log(myRequest.inventorys);
				console.log(myRequest);
			}else{
				alert("输入的数量无效。");
			}
	}
}


function scan(){
	if($('#count_scan').val()!=""){
		var a= myRequest.scan($('#count_scan').val());
		if(a==0){
			alert("此条形码 "+$('#count_scan').val()+"不在入仓盘点表里\n请重新扫描");
		}else if(a==-1){
			alert("此条形码 "+$('#count_scan').val()+"已盘点过的数目已经等于计划数目\n请重新扫描");
		}else{
			//$('#count_scan').focus();
		}
		$('#count_scan').val("");
		$('#count_scan').focus();
	}
}

function stack(){
	var ready=myRequest.isReady();
	if(!ready){
		alert("盘点没完成");
	}else{
			jQuery.ajax({
				  type: "POST",
				  url: "api/",
				  dataType : 'json',   
				  data:{	action:myRequest.action,
							userid:myRequest.userid,
							orgid:myRequest.orgid,
							inventorys:myRequest.inventorys},
			  	  beforeSend: function ( xhr ) {
			 		$('.dim').show();
				 	},
				  complete: function(jqXHR) {
				  	   if(jqXHR.readyState != 4) {
				  	   	alert(服务器通讯或者运算出现问题);
				  	   		 $('.dim').hide();
      					 }  
				  	//alert(data['stat']);
				  
 
	    			}        
			}).done(function ( data ) {
								 $('.dim').hide();

				if(data['stat']==0){
					alert('递交参数不完整');

				}else{
					var stackedNum=data['stackedNum'];
					var failededNum=data['failededNum'];
					var error=data['error'];
					var errorName="";
					for (var i = 0; i < error.length; i++) {
						errorName+="\n"+error[i];
					}
					alert("成功入库了"+stackedNum+"种产品,失败了 "+failededNum+"产品。"+errorName);
				}
			});
			myRequest.dump();
			document.location.reload(true);
		}
}

</script>
	<div class="block">

	<div class="invertedshiftdown">
<ul>
	<?php
	if($organization->type=='s'){
	?>
			<li><a href="?page=organization_inventory&id=<?php echo $organization->id; ?>">查看货存</a></li>
			<li><a href="?page=organization_sell&id=<?php echo $organization->id; ?>">出售</a></li>
			<li><a href="?page=organization_return&id=<?php echo $organization->id; ?>">退款</a></li>
			<li class="current"><a href="?page=organization_distribute&id=<?php echo $organization->id; ?>">分配</a></li>
			<li><a href="?page=organization_distribute_confirm&id=<?php echo $organization->id; ?>">处理待定分配</a></li>
			<li><a href="?page=organization_transaction&id=<?php echo $organization->id; ?>">查看物流记录</a></li>
			<li><a href="?page=organization_sell_record&id=<?php echo $organization->id; ?>">查看销售记录</a></li>
			<li><a href="?page=organization_consume&id=<?php echo $organization->id; ?>">报告损耗/遗失</a></li>
			<li><a href="?page=organization_count&id=<?php echo $organization->id; ?>">货存盘点</a></li>
	<?php
	}elseif($organization->type=='w'){
	?>
	       	<li><a href="?page=organization_inventory&id=<?php echo $organization->id; ?>">查看库存</a></li>
			<li><a href="?page=organization_stack&id=<?php echo $organization->id; ?>">入库</a></li>
			<li class="current"><a href="?page=organization_distribute&id=<?php echo $organization->id; ?>">分配</a></li>
			<li><a href="?page=organization_distribute_confirm&id=<?php echo $organization->id; ?>">处理待定分配</a></li>
			<li><a href="?page=organization_transaction&id=<?php echo $organization->id; ?>">查看物流记录</a></li>
			<li><a href="?page=organization_consume&id=<?php echo $organization->id; ?>">报告损耗/遗失</a></li>
			<li><a href="?page=organization_count&id=<?php echo $organization->id; ?>">货存盘点</a></li>
    <?php
	}
	?>
</ul>




<form id="myform">
<input type="text" class="textinput" /> <input class="submit" type="submit" value="搜索" />
</form>
</div>

<br style="clear: both;" />
<div id= 'content'>

	<?php
	//print_r($_GET);
	if(isset($_GET['stat'])){
		if($_GET['stat']=='distributed'){
			
		
	
	?>
	<div class="isa_success">
		<h4>操作成功：</h4>分配
	</div>
	
	<?php
		}elseif($_GET['stat']=='error'&&$_GET['error']=='limit'){
	?>
	<div class="isa_warning">
		<h4>操作失败：</h4>无法分配（货存数量有限）
	</div>
	
		<?php
		}elseif($_GET['stat']=='error'&&$_GET['error']=='badData'){
	?>
	<div class="isa_warning">
		<h4>操作失败：</h4>无法分配（提交了无效数据）
	</div>
<?php

		}
}
?>

		<h2><?php echo $organization->name ?>》库存分配界面</h2>
	<h4>第一步，请从下列“库存详情表”中选择所要分配的产品（按单选按钮）</h4>

        	<input type="hidden" id='action' name="action" value="organization_distribute" />
        	<input type="hidden" id='user1' name="user1" value="<?php echo $currentUser->id ?>" />
        	<input type="hidden" id='source' name="source" value="<?php echo  $organization->id ?>" />  
    	            	<?php echo $table;?>
    	            	<br>
    	            	    	<h4>第二步,选择分配目的地然后按“添加到分配计划表”按钮</h4>
    	<div class="important">若要分配货物的形码若没能在上面列表找到请联络管理员</div>
		   <div class="element" style="clear:both;">
            <label class="form">选择分配到：</label>
		   <select id="target" name="target">
             <?php
             foreach ($allOrganizations as  $org) {
             	$id = $org->id;
             	$name =$org->name;
				
				if($id!=$organization->id){
             ?>
             <option value='<?php echo $id;?>' ><?php echo $name;?></option>
             <?php
                }
              }
             ?>
           </select>
             </div>
             <?php
if(count($organization->getInventory())){
             ?>
                     <div style="padding-left:40%">
    					<div class="button blue" onclick="addPlan()">添加到分配计划表</div>
    				  </div>
               

             <?php
}else{
             ?>
  				<div style="padding-left:40%">
    					此机构无可用库存，无法分配。
    			</div>
             <?php
}
             ?>

		    			<br><div style="padding-left:45%;padding-right:50%"><img src="images/arrow_down.png"></div>
						<br>
<style type='text/css' title='currentStyle'>
					@import 'css/demo_page.css';
					@import 'css/demo_table.css';
				</style>
				<div class='tableBackground'>
				<strong>分配盘点</strong><br>
			<table cellpadding='0' cellspacing='0' border='0' class='display' id='stackTable' width='100%'>
				<thead>
					<tr>
				      	<th>计划分配数量</th>
				      	<th>已扫描数量</th>
						<th>扫描条码</th>
						<th>生产款号</th>
						<th>设计版号</th>
						<th>类型</th>
				        <th>年份</th>
						<th>季节</th>
						<th>流水号</th>
						<th>面料</th>
						<th>色彩</th>
						<th>尺寸</th>
						<th>设计师</th>
						<th>原价</th>
						<th>折率</th>
						<th>现售与否</th>
						<th>产品名</th>
						<th>分配到</th>
					</tr>
				</thead>
				<tbody>
					
				</tbody>
				<tfoot>
				  <tr>
				  	<th>计划分配数量</th>
		      		<th>已盘点数量</th>
					<th>扫描条码</th>
					<th>生产款号</th>
					<th>设计版号</th>
					<th>类型</th>
			        <th>年份</th>
					<th>季节</th>
					<th>流水号</th>
					<th>面料</th>
					<th>色彩</th>
					<th>尺寸</th>
					<th>设计师</th>
					<th>原价</th>
					<th>折率</th>
					<th>现售与否</th>
					<th>产品名</th>
					<th>分配到</th>
				   </tr>
				</tfoot>
			</table>
			</div>
				<div class="important">重复做第一，第二步直到您已经计划好您要做的分配，然后再做第三步</div>
   	<h4>第三步。盘点货物，必须一件件扫描 直到上面盘点表中所有计划分配数目和已扫描数目对齐（红色边框变绿色为止）</h4>

				<div style="padding-left:30%"><input id="count_scan" ype="text" maxlength="14" size="18">
					<div class="button blue" onclick="scan()">盘点扫描</div></div>
				<br>				<div style="padding-left:40%;padding-right:50%"><img src="images/arrow_down.png"></div>
				   	<h4>第四步。盘点完毕后，记得按这个键做入库（否则不算）</h4>

				<br><div style="padding-left:40%"><button class="button blue" onclick="stack()">盘点完毕分配</div></div>


<br>
<br>
<br>


   
<div class="clear"></div>
	</div>